package com.raindrop.utils;

import java.util.regex.Pattern;

/**
 * Created by liuhang on 2017/4/21.
 * 规则工具 正则检测
 */

public class RegularUtil {
    private static final String RULE_EMAIL = "^[A-Za-z0-9\\u4e00-\\u9fa5]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$";
    private static final String RULE_PHONE = "^0?(13[0-9]|15[012356789]|17[013678]|18[0-9]|14[57])[0-9]{8}$";
    private static final String RULE_ID = "(^[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{2}$)";
    private static final String RULE_PASS_PORT = "^1[45][0-9]{7}|G[0-9]{8}|P[0-9]{7}|S[0-9]{7,8}|D[0-9]+$";

    public static boolean isEmail(String email, Integer size) {
        if (size == null || email.length() > size)
            return false;
        return Pattern.matches(RULE_EMAIL, email);
    }

    public static boolean isEmail(String email) {
        return Pattern.matches(RULE_EMAIL, email);
    }

    public static boolean isPhone(String phone) {
        return Pattern.matches(RULE_PHONE, phone);
    }

    public static boolean isIDCard(String idCard) {
        return Pattern.matches(RULE_ID, idCard);
    }

    public static boolean isPassPort(String passPort) {
        return Pattern.matches(RULE_PASS_PORT, passPort);
    }

    public static void main(String[] args) {
//        boolean result = isEmail("123@qq.com");
        boolean result = Pattern.matches("[a-z0-9]+([._\\\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$", "123@qq.com");
        System.out.println("args = [" + result + "]");
    }
}
